package com.wxy.demo;

import com.wxy.demo.dao.*;
import com.wxy.demo.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Optional;

@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        Environment environment = context.getBean(Environment.class);
        String port = Optional.ofNullable(environment.getProperty("server.port")).orElse("8080");
        String path = Optional.ofNullable(environment.getProperty("server.servlet.context-path")).orElse("/");
        String host = Optional.ofNullable(InetAddress.getLocalHost().getHostAddress()).orElse("localhost");
        log.info("访问地址：http://{}:{}{}", host, port, path);
    }

    /**
     * 加密算法
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private RolePermissionRepository rolePermissionRepository;

    @Autowired
    private UserRoleRepository userRoleRepository;

    @PostConstruct
    public void run() {
        if (permissionRepository.count() == 0) {
            // 创建权限
            Permission permission = permissionRepository.save(new Permission().setName("查询").setCode("findAll"));
            Permission permission1 = permissionRepository.save(new Permission().setName("新增").setCode("add"));
            // 创建角色
            Role role = roleRepository.save(new Role().setName("管理员"));
            Role role1 = roleRepository.save(new Role().setName("游客"));
            // 创建权限角色关联
            rolePermissionRepository.save(new RolePermission().setRoleId(role.getId()).setPermissionId(permission.getId()));
            rolePermissionRepository.save(new RolePermission().setRoleId(role.getId()).setPermissionId(permission1.getId()));
            rolePermissionRepository.save(new RolePermission().setRoleId(role1.getId()).setPermissionId(permission.getId()));
            // 创建用户
            User user = userRepository.save(new User().setUsername("admin").setPassword(passwordEncoder().encode("admin")));
            User user1 = userRepository.save(new User().setUsername("visitor").setPassword(passwordEncoder().encode("visitor")));
            // 创建用户角色关联
            userRoleRepository.save(new UserRole().setUserId(user.getId()).setRoleId(role.getId()));
            userRoleRepository.save(new UserRole().setUserId(user1.getId()).setRoleId(role1.getId()));
        }
    }
}
